package tools;

import java.util.List;

import org.jdom.Element;

public class readconfig {

	private static   readconfig  _config = new readconfig();	
	public static  readconfig GetInstance() { return _config; }
	private static DealXml _xml ;
	
	public readconfig(){
		_xml = new DealXml("/config/config.xml", true);
		if (_xml.IsParseOK()){
			System.out.println("ok");
			//String s = _xml.GetXmlStr();
			//System.out.println(s);
		}
	}
	
	/*
	 * 获取所有数据根路径
	 */
	public String GetRootDir()
	{
		return _xml.GetRootPropertyValue("dir");
	}
	
	/*
	 * 获取数据的路径
	 */
	public String GetdataDir(String timescale,String type,String element)
	{
		String tmp = new String();
		//没办法XMl节点不能以数字开头
		if(timescale.compareTo("4daily")==0){
			tmp = "tran4daily";
		}else{
			tmp = timescale;
		}
		//先检查输入的参数是否存在于XML
		if(_xml.IsHave(tmp,type,element)!=null){
			return GetRootDir()+timescale+"/"+type+"/"+element+"/";
		}else{
			return null;
		}	
	}

	/*
	 * 获取数据根节点的所有子节点名字（第一级所有节点名称）
	 */
	public String[] GetTimeScaleList()
	{
		List<Element> ls = _xml.GetRootElement().getChildren();
		int len = ls.size();
		String[] cLs = new String[len];
		for(int i=0;i<len;i++)
		{
			if(ls.get(i).getName().compareTo("tran4daily")==0){
				cLs[i]="4daily";
			}else{
				cLs[i]=ls.get(i).getName();
			}		
		}
		return cLs;
	}
	
	/*
	 * 获取数据第二级子节点名字
	 */
	public String[] GetTypeList(String timescale)
	{
		String tmp = new String();
		//没办法XMl节点不能以数字开头
		if(timescale.compareTo("4daily")==0){
			tmp = "tran4daily";
		}else{
			tmp = timescale;
		}
		
		if(_xml.IsHave(tmp,null,null)==null){
			return null;
		}else{
			List<Element> ls =_xml.IsHave(tmp,null,null).getChildren();
			int len = ls.size();
			String[] cLs = new String[len];
			for(int i=0;i<len;i++)
			{
				cLs[i]=ls.get(i).getName();
			}
			return cLs;
		}
		
	}
	
	/*
	 * 获取数据第三级子节点名字
	 */
	public String[] GetElementList(String timescale,String type)
	{
		String tmp = new String();
		//没办法XMl节点不能以数字开头
		if(timescale.compareTo("4daily")==0){
			tmp = "tran4daily";
		}else{
			tmp = timescale;
		}
		
		if(_xml.IsHave(tmp,type,null)==null){
			return null;
		}else{
			List<Element> ls =_xml.IsHave(tmp,type,null).getChildren();
			int len = ls.size();
			String[] cLs = new String[len];
			for(int i=0;i<len;i++)
			{
				cLs[i]=ls.get(i).getName();
			}
			return cLs;
		}
		
	}
	
	/*
	 * 获取数据经度
	 */
	public String GetdataLon(String timescale,String type,String element)
	{
		return GetdataInfo(timescale,type,element).getChildText("lon");
	}
	
	/*
	 * 获取数据纬度
	 */
	public String GetdataLat(String timescale,String type,String element)
	{
		return GetdataInfo(timescale,type,element).getChildText("lat");
	}
	
	/*
	 * 获取层信息
	 */
	public String GetdataLayer(String timescale,String type,String element)
	{
		return GetdataInfo(timescale,type,element).getChildText("layer");
	}
	
	/*
	 * 获取数据信息
	 */
	public Element GetdataInfo(String timescale,String type,String element)
	{
		String tmp = new String();
		//没办法XMl节点不能以数字开头
		if(timescale.compareTo("4daily")==0){
			tmp = "tran4daily";
		}else{
			tmp = timescale;
		}
		//先检查输入的参数是否存在于XML
		if(_xml.IsHave(tmp,type,element)==null){
			return null;
		}else{
			return _xml.IsHave(tmp,type,element);
		}	
	}
}
